

def get_breadcrumb(category):
    """
    面包屑导航
    :param category: 商品类别
    :return: 面包屑导航字典，值为商品类别对象
    """

    # {
    #     "cat1":
    #     "cat2":
    #     "cat3":
    # }
    breadcrumb_dic = {}
    # 判断是几级类别，mysql的空是null，python里是None
    if category.parent is None:  #  一级类别
        breadcrumb_dic["cat1"] = category
    elif category.subs.count() == 0:    # 三级类别
        breadcrumb_dic = {
            "cat1": category.parent.parent,
            "cat2": category.parent,
            "cat3": category
        }

    else:   # 二级类别
        breadcrumb_dic = {
            "cat1": category.parent,
            "cat2": category
        }

    return breadcrumb_dic
